R Coding Workshop: 4th Meeting

GIS & Geospatial Data Analysis (Fall 2025)

R Coding Workshop: 4th Meeting

Outline

  • R Functions
  • dplyr functions
    • arrange()
    • slice_sample()
  • define your own function
  • ggplot2

R Functions

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.3.0
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(mapview)
library(rnaturalearth)
library(viridis)
Le chargement a nécessité le package : viridisLite

Using base and imported functions

Functions from Base R

class(print) |> print()
[1] "function"
class(class)
[1] "function"
long_df <- tibble(
    value = 1:37
)
print(long_df)
# A tibble: 37 × 1
   value
   <int>
 1     1
 2     2
 3     3
 4     4
 5     5
 6     6
 7     7
 8     8
 9     9
10    10
# ℹ 27 more rows
long_df |> print(n = 37)
# A tibble: 37 × 1
   value
   <int>
 1     1
 2     2
 3     3
 4     4
 5     5
 6     6
 7     7
 8     8
 9     9
10    10
11    11
12    12
13    13
14    14
15    15
16    16
17    17
18    18
19    19
20    20
21    21
22    22
23    23
24    24
25    25
26    26
27    27
28    28
29    29
30    30
31    31
32    32
33    33
34    34
35    35
36    36
37    37

Imported functions

class(select)
[1] "function"
class(mapview)
[1] "standardGeneric"
attr(,"package")
[1] "methods"

Components of a Function

When we want to write our own Function

  • copy-and-paste
  • vector operations
  • creating plots

Components of a Function

  • Function Name: Usually a verb
  • Parameters: Variable provided when you defined your function,they act as a placeholder for expected input
  • Arguments: Variable specified at the time you use the function
  • Return: Output of the function
add_four <- function(x) x + 4
add_four |> class() |> print()
[1] "function"
add_four <- function(x) x + 4
add_four |> class() |> print()
[1] "function"
a <- 1.46
add_four(a)
[1] 5.46
v1 <- c(1, 9, 8, 8)
add_four(v1)
[1]  5 13 12 12
v1 + 4
[1]  5 13 12 12
sapply(v1, add_four)
[1]  5 13 12 12
long_df$value |> add_four()
 [1]  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[26] 30 31 32 33 34 35 36 37 38 39 40 41
book_challenge_sf <- st_read('data/book-challenges-state-sf.gpkg')
Reading layer `book-challenges-state-sf' from data source 
  `/Users/toyuan/dohnanyi/data/book-challenges-state-sf.gpkg' 
  using driver `GPKG'
Simple feature collection with 51 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1435 ymin: 18.90612 xmax: 179.7809 ymax: 71.4125
Geodetic CRS:  WGS 84
book_challenge_sf |> mapview(zcol = 'count')

Getting familiar with how functions work

  • paste() and paste0() function
create_label <- function(unit, count) {
    paste0(unit, ': ', count)
}
book_challenge_sf <- book_challenge_sf |> mutate(lab = create_label(gn_name, count))
book_challenge_sf |> mapview(zcol = 'count', label = 'lab')

Plotting Function

book_challenge_sf |>
  filter(!(postal %in% c('HI', 'AK'))) |>
  ggplot() +
  geom_sf(
    aes(fill = count),
    color = 'white',
    size = 0.1
  ) +
  geom_sf_label(
    aes(label = postal),
    size = 2.5,
    fill = 'white',
    alpha = 0.7,
    color = 'black'
  ) +
  scale_fill_viridis(na.value = '#E2E2E2') +
  theme_minimal() +
  labs(
    title = 'Book Challenges by State',
    subtitle = '2000-2010'
  )
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data

plot_state_choropleth <- function(sf, col_str, plt_title) {
    sf |>
    filter(!(postal %in% c('HI', 'AK'))) |>
    ggplot() +
    geom_sf(
      aes_string(fill = col_str),
      color = 'white',
      size = 0.1
    ) +
    geom_sf_label(
      aes(label = postal),
      size = 2.5,
      fill = 'white',
      alpha = 0.7,
      color = 'black'
    ) +
    scale_fill_viridis(na.value = '#E2E2E2') +
    theme_minimal() +
    labs(
      title = plt_title,
      subtitle = '2000-2010'
    )
}
plot_state_choropleth(
    book_challenge_sf,
    'count',
    'Book Challenges by State'
    )
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data

plot_state_choropleth(book_challenge_sf, 'median_income', 'State Median Income')
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data

book_challenge_sf |> plot_state_choropleth('political_value_index', 'Political Value Index')
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data

arrange()

  • Sort observations by column value
book_challenge_sf |>
  arrange(desc(count)) |>
  pull(lab)
 [1] "Pennsylvania: 147"        "Oregon: 118"             
 [3] "Colorado: 80"             "California: 50"          
 [5] "Illinois: 39"             "Michigan: 34"            
 [7] "Virginia: 33"             "Ohio: 29"                
 [9] "Florida: 26"              "New York: 24"            
[11] "Oklahoma: 23"             "North Carolina: 20"      
[13] "Indiana: 20"              "Minnesota: 17"           
[15] "South Carolina: 15"       "Idaho: 14"               
[17] "Vermont: 13"              "Georgia: 13"             
[19] "Kansas: 13"               "Iowa: 13"                
[21] "Kentucky: 13"             "Tennessee: 13"           
[23] "Arizona: 12"              "Louisiana: 12"           
[25] "New Jersey: 11"           "Missouri: 11"            
[27] "Alabama: 10"              "Wisconsin: 10"           
[29] "Washington: 9"            "Alaska: 9"               
[31] "Massachusetts: 8"         "Montana: 7"              
[33] "New Hampshire: 7"         "North Dakota: 6"         
[35] "Connecticut: 6"           "Maryland: 5"             
[37] "Arkansas: 5"              "Wyoming: 4"              
[39] "Maine: 3"                 "New Mexico: 3"           
[41] "Rhode Island: 3"          "South Dakota: 3"         
[43] "West Virginia: 3"         "Delaware: 2"             
[45] "Nebraska: 2"              "Mississippi: 1"          
[47] "Utah: 1"                  "Texas: NA"               
[49] "District of Columbia: NA" "Hawaii: NA"              
[51] "Nevada: NA"              
book_challenge_sf |>
  arrange(political_value_index) |>
  select(gn_name, count, political_value_index)
gn_name count political_value_index geom
Utah 1 -20.2 MULTIPOLYGON (((-111.0502 4…
Wyoming 4 -19.7 MULTIPOLYGON (((-109.0463 4…
Idaho 14 -17.4 MULTIPOLYGON (((-117.0382 4…
Oklahoma 23 -16.9 MULTIPOLYGON (((-103.0002 3…
Nebraska 2 -13.5 MULTIPOLYGON (((-104.0537 4…
Alaska 9 -13.4 MULTIPOLYGON (((-141.0056 6…
Alabama 10 -13.2 MULTIPOLYGON (((-87.41958 3…
Kansas 13 -11.5 MULTIPOLYGON (((-102.0396 3…
North Dakota 6 -10.4 MULTIPOLYGON (((-104.0476 4…
Kentucky 13 -10.4 MULTIPOLYGON (((-89.42446 3…
Louisiana 12 -9.7 MULTIPOLYGON (((-89.52599 3…
Mississippi 1 -9.5 MULTIPOLYGON (((-88.40221 3…
South Dakota 3 -8.9 MULTIPOLYGON (((-104.0567 4…
Arkansas 5 -8.8 MULTIPOLYGON (((-90.30422 3…
Tennessee 13 -8.7 MULTIPOLYGON (((-90.30422 3…
West Virginia 3 -7.9 MULTIPOLYGON (((-82.58945 3…
South Carolina 15 -7.8 MULTIPOLYGON (((-78.57316 3…
Montana 7 -7.1 MULTIPOLYGON (((-116.0482 4…
Georgia 13 -6.8 MULTIPOLYGON (((-80.89029 3…
Indiana 20 -6.2 MULTIPOLYGON (((-84.80608 4…
Arizona 12 -6.1 MULTIPOLYGON (((-111.0063 3…
North Carolina 20 -4.3 MULTIPOLYGON (((-76.03173 3…
Missouri 11 -3.1 MULTIPOLYGON (((-95.31725 4…
Florida 26 -1.8 MULTIPOLYGON (((-87.44734 3…
Virginia 33 -1.7 MULTIPOLYGON (((-76.01325 3…
Ohio 29 -0.7 MULTIPOLYGON (((-80.52023 4…
Colorado 80 -0.2 MULTIPOLYGON (((-109.0463 4…
Iowa 13 1.0 MULTIPOLYGON (((-96.48266 4…
New Hampshire 7 1.6 MULTIPOLYGON (((-71.50585 4…
Pennsylvania 147 2.0 MULTIPOLYGON (((-79.76301 4…
Minnesota 17 2.3 MULTIPOLYGON (((-97.22609 4…
New Mexico 3 2.4 MULTIPOLYGON (((-108.1375 3…
Wisconsin 10 2.4 MULTIPOLYGON (((-87.80425 4…
Michigan 34 3.8 MULTIPOLYGON (((-84.4913 46…
Oregon 118 4.0 MULTIPOLYGON (((-124.4924 4…
New Jersey 11 4.4 MULTIPOLYGON (((-75.54133 3…
Washington 9 5.0 MULTIPOLYGON (((-122.753 48…
Maine 3 5.5 MULTIPOLYGON (((-71.08495 4…
Delaware 2 7.0 MULTIPOLYGON (((-75.05809 3…
Connecticut 6 7.1 MULTIPOLYGON (((-73.6417 41…
California 50 7.4 MULTIPOLYGON (((-114.7243 3…
Illinois 39 7.7 MULTIPOLYGON (((-89.1237 36…
Maryland 5 8.5 MULTIPOLYGON (((-75.64786 3…
New York 24 10.2 MULTIPOLYGON (((-79.06523 4…
Rhode Island 3 11.2 MULTIPOLYGON (((-71.23686 4…
Massachusetts 8 11.7 MULTIPOLYGON (((-71.19396 4…
Vermont 13 13.4 MULTIPOLYGON (((-73.35134 4…
Texas NA NA MULTIPOLYGON (((-103.3115 2…
District of Columbia NA NA MULTIPOLYGON (((-77.02293 3…
Hawaii NA NA MULTIPOLYGON (((-154.8996 1…
Nevada NA NA MULTIPOLYGON (((-114.0425 4…
book_challenge_df <- read_csv('data/book-challenge11.csv')
Rows: 931 Columns: 17
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr   (3): title, author, state
dbl  (13): book_id, year, removed, explicit, antifamily, occult, language, l...
date  (1): date

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
book_challenge_df |> arrange(author, title) |> head(10)
title book_id author date year removed explicit antifamily occult language lgbtq violent state political_value_index median_income hs_grad_rate college_grad_rate
Go and Come Back 721 Abelove, Joan 2000-06-18 2000 0 1 0 0 0 0 0 WV -7.9 -7290.0 -4.361958 -9.2237299
Finding Our Way–The Teen Girl Survival Guide 695 Abner, Allison 2002-05-21 2002 0 1 0 0 0 0 0 AZ -6.1 3490.5 1.438042 -0.5237299
Death Blossoms 522 Abu-Jamal, Mumia 2005-07-03 2005 0 0 0 0 0 0 0 OR 4.0 1274.5 5.538042 1.0762701
Tijuana Bibles 1811 Adelman, Bob 2000-06-29 2000 0 1 0 0 0 0 1 OR 4.0 1274.5 5.538042 1.0762701
Art for Lovers 160 Adler, Sabine 2005-04-08 2005 0 0 0 0 0 0 0 OR 4.0 1274.5 5.538042 1.0762701
Psychic Academy Series 1484 Aki, Katsu 2008-06-07 2008 0 0 0 0 0 0 0 VA -1.7 11296.0 1.938042 5.4762701
Monkey High Vol. 3 1277 Akira, Shouko 2009-05-06 2009 0 0 0 0 0 0 0 MI 3.8 2966.0 3.838042 -2.2237299
Absolutely True Diary of a Part-Time Indian, The 69 Alexie, Sherman 2008-10-02 2008 1 0 0 0 0 0 0 IA 1.0 3921.5 6.538042 -2.8237299
Absolutely True Diary of a Part-Time Indian, The 69 Alexie, Sherman 2009-05-04 2009 0 1 0 0 1 0 0 IL 7.7 6489.5 1.838042 2.0762701
Absolutely True Diary of a Part-Time Indian, The 69 Alexie, Sherman 2010-05-12 2010 1 0 0 0 1 0 1 MO -3.1 1279.5 1.738042 -2.4237299

slice_sample()

book_challenge_df |> slice_sample(n = 10)
title book_id author date year removed explicit antifamily occult language lgbtq violent state political_value_index median_income hs_grad_rate college_grad_rate
Summer of My German Soldier 1739 Greene, Bette 2002-04-08 2002 0 0 0 0 1 0 0 AR -8.8 -6258.5 -4.2619579 -7.32373
Between a Rock and a Hard Place 223 Carter, Alden 2000-06-29 2000 0 0 0 0 1 0 0 OR 4.0 1274.5 5.5380421 1.07627
Whale Talk 1941 Crutcher, Chris 2007-10-25 2007 1 0 0 0 1 0 0 OH -0.7 2469.0 3.4380421 -2.92373
Harry Potter and the Chamber of Secrets 868 Rowling, J.K. 2000-03-01 2000 1 0 0 1 0 0 0 MI 3.8 2966.0 3.8380421 -2.22373
Boy Gets Girl: A Play 295 Gilman, Rebecca 2005-08-26 2005 1 0 0 0 1 0 0 NY 10.2 5007.5 -0.4619579 3.37627
Adventures of Huckleberry Finn, The 75 Twain, Mark 2007-08-02 2007 0 0 0 0 0 0 0 MN 2.3 15378.0 8.3380421 3.37627
We All Fall Down 1930 Cormier, Robert 2004-01-12 2004 1 0 0 0 1 0 1 KS -11.5 144.5 6.4380421 1.77627
I Am Charlotte Simmons 950 Wolfe, Tom 2006-07-28 2006 0 1 0 0 1 0 0 MO -3.1 1279.5 1.7380421 -2.42373
Adventures of Tom Sawyer, The 78 Twain, Mark 2007-08-02 2007 0 0 0 0 1 0 0 IA 1.0 3921.5 6.5380421 -2.82373
Tamar 1755 Chamberlain, Ann 2002-09-17 2002 0 0 0 0 0 0 0 OR 4.0 1274.5 5.5380421 1.07627
book_challenge_df |>
  filter(state == 'PA') |>
  slice_sample(n = 10)
title book_id author date year removed explicit antifamily occult language lgbtq violent state political_value_index median_income hs_grad_rate college_grad_rate
Mythology 1012 NA 2001-04-25 2001 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Breath 312 Winton, Tim 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Starplace, The 1699 Grove, Vicki 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Beyond the Chocolate War 228 Cormier, Robert 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Love & Sex 1183 Cart, Michael 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
God Box, The 724 Sanchez, Alex 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
As I Lay Dying 165 Faulkner, William 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Braless in Wonderland 307 Fischer, Debbie Reed 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Keeping You A Secret 1069 Peters, Julie Anne 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Like Sisters on the Homefront 1139 Williams-Garcia, Rita 2000-08-28 2000 1 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373